import { createPostAPI, loadPostsAPI, likePostAPI } from "../../apis/post.js";

export const post = {
  state() {
    return {
      posts: [],
      text: "我是详细内容",
      currentPostIndex: null,
      files: [
        {
          src: "/src/assets/large_screenshot_20170508_170345_cf6bb41a78.png",
          id: 1,
          like: true,
          favor: true,
        },
        {
          src: "/src/assets/large_screenshot_20170508_170345_cf6bb41a78.png",
          id: 2,
          like: false,
          favor: true,
        },
        {
          src: "/src/assets/large_screenshot_20170508_170345_cf6bb41a78.png",
          id: 3,
          like: true,
          favor: false,
        },
        {
          src: "/src/assets/large_screenshot_20170508_170345_cf6bb41a78.png",
          id: 4,
          like: true,
          favor: true,
        },
        {
          src: "/src/assets/large_screenshot_20170508_170345_cf6bb41a78.png",
          id: 5,
          like: true,
          favor: false,
        },
        {
          src: "/src/assets/large_screenshot_20170508_170345_cf6bb41a78.png",
          id: 6,
          like: true,
          favor: false,
        },
        {
          src: "/src/assets/large_screenshot_20170508_170345_cf6bb41a78.png",
          id: 7,
          like: true,
          favor: true,
        },
        {
          src: "/src/assets/large_screenshot_20170508_170345_cf6bb41a78.png",
          id: 8,
          like: true,
          favor: true,
        },
      ],
      originFiles: [
        {
          name: "rose",
          pubDate: +new Date(),
          description: "一棵树一棵树",
        },
        {
          name: "rose",
          pubDate: +new Date(),
          description: "二棵树二棵树",
        },
        {
          name: "rose",
          pubDate: +new Date(),
          description: "三棵树三棵树",
        },
        {
          name: "rose",
          pubDate: +new Date(),
          description: "四棵树四棵树",
        },
        {
          name: "rose",
          pubDate: +new Date(),
          description: "五棵树五棵树",
        },
        {
          name: "rose",
          pubDate: +new Date(),
          description: "六棵树六棵树",
        },
        {
          name: "rose",
          pubDate: +new Date(),
          description: "七棵树七棵树",
        },
        {
          name: "rose",
          pubDate: +new Date(),
          description: "八棵树八棵树",
        },
      ],
      searchTerm: [],
    };
  },
  mutations: {
    //search模块
    changeSearchTerm(state, val) {
      state.searchTerm = state.originFiles.filter((item) => {
        return item.description.includes(val) || item.name.includes(val);
      });
    },
    updatePosts(state, val) {
      state.posts = val;
    },
    togglePostLike(state, { id, isLike }) {
      const item = state.posts.find((item) => {
        return item.id === id;
      });
      if (isLike) {
        item.likes = (item.likes || 0) + 1;
      } else {
        item.likes--;
      }
      item.likedByMe = isLike;
    },
    setCurrentId(state, val) {
      state.currentPostIndex = val;
    },
    addCommentNum(state) {
      state.text + "1";
    },
  },
  getters: {
    getDetailPost(state) {
      return state.text + state.currentPostIndex;
    },
  },
  actions: {
    async uploadImagePost(context, { image, description }) {
      await createPostAPI(image, description);
      context.dispatch("getPosts");
      context.commit("changeUploadVal", false);
    },
    async getPosts(context) {
      const res = await loadPostsAPI();
      context.commit("updatePosts", res);
    },

    async toggleLike({ commit, dispatch }, id) {
      const isLike = await likePostAPI(id);
      commit("togglePostLike", { id, isLike });
    },
    showPostDetail(context, val) {
      // currentPostIndex = val;
      // console.log(val);
      context.commit("setCurrentId", val);
      context.commit("changePostDetail", true);
    },
    hidePostDetail(context) {
      context.commit("changePostDetail", false);
    },
    async addComment(context, { comment, postID }) {
      //addPOSTNUM
      context.commit("addCommentNum");
      // await createCommentAPI(comment, postID);
    },
  },
};
